home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / prog / dflt18.arj / DIRECT.C < prev    next >
C/C++ Source or Header  |  1994-01-25  |  5KB  |  194 lines

  1. /* ---------- direct.c --------- */
  2.  
  3. #include "dflat.h"
  4.  
  5. static char path[MAXPATH];
  6. static char drive[MAXDRIVE] = " :";
  7. static char dir[MAXDIR];
  8. static char name[MAXFILE];
  9. static char ext[MAXEXT];
  10.  
  11. /* ----- Create unambiguous path from file spec, filling in the
  12.      drive and directory if incomplete. Optionally change to
  13.      the new drive and subdirectory ------ */
  14. void CreatePath(char *spath,char *fspec,int InclName,int Change)
  15. {
  16.     int cm = 0;
  17.     unsigned currdrive;
  18.     char currdir[MAXPATH+1];
  19.     char *cp;
  20.  
  21.     if (!Change)    {
  22.         /* ---- save the current drive and subdirectory ---- */
  23.         currdrive = getdisk();
  24.         getcwd(currdir, sizeof currdir);
  25.         memmove(currdir, currdir+2, strlen(currdir+1));
  26.         cp = currdir+strlen(currdir)-1;
  27.         if (*cp == '\\')
  28.             *cp = '\0';
  29.     }
  30.     *drive = *dir = *name = *ext = '\0';
  31.     fnsplit(fspec, drive, dir, name, ext);
  32.     if (!InclName)
  33.         *name = *ext = '\0';
  34.     *drive = toupper(*drive);
  35.     if (*ext)
  36.         cm |= EXTENSION;
  37.     if (InclName && *name)
  38.         cm |= FILENAME;
  39.     if (*dir)
  40.         cm |= DIRECTORY;
  41.     if (*drive)
  42.         cm |= DRIVE;
  43.     if (cm & DRIVE)
  44.         setdisk(*drive - 'A');
  45.     else     {
  46.         *drive = getdisk();
  47.         *drive += 'A';
  48.     }
  49.     if (cm & DIRECTORY)    {
  50.         cp = dir+strlen(dir)-1;
  51.         if (*cp == '\\')
  52.             *cp = '\0';
  53.         chdir(dir);
  54.     }
  55.     getcwd(dir, sizeof dir);
  56.     memmove(dir, dir+2, strlen(dir+1));
  57.     if (InclName)    {
  58.         if (!(cm & FILENAME))
  59.             strcpy(name, "*");
  60.         if (!(cm & EXTENSION) && strchr(fspec, '.') != NULL)
  61.             strcpy(ext, ".*");
  62.     }
  63.     else
  64.         *name = *ext = '\0';
  65.     if (dir[strlen(dir)-1] != '\\')
  66.         strcat(dir, "\\");
  67.     if (spath != NULL)
  68.         fnmerge(spath, drive, dir, name, ext);
  69.     if (!Change)    {
  70.         setdisk(currdrive);
  71.         chdir(currdir);
  72.     }
  73. }
  74.  
  75. static int dircmp(const void *c1, const void *c2)
  76. {
  77.     return stricmp(*(char **)c1, *(char **)c2);
  78. }
  79.  
  80. static BOOL BuildList(WINDOW wnd, char *fspec, BOOL dirs)
  81. {
  82.     int ax, i = 0, criterr = 1;
  83.     struct ffblk ff;
  84.     CTLWINDOW *ct = FindCommand(wnd->extension,
  85.                             dirs ? ID_DIRECTORY : ID_FILES,LISTBOX);
  86.     WINDOW lwnd;
  87.     char **dirlist = NULL;
  88.  
  89.     if (ct != NULL)    {
  90.         lwnd = ct->wnd;
  91.         SendMessage(lwnd, CLEARTEXT, 0, 0);
  92.  
  93.            while (criterr == 1)    {
  94.                ax = findfirst(fspec, &ff, dirs ? FA_DIREC: 0);
  95.                criterr = TestCriticalError();
  96.            }
  97.            if (criterr)
  98.                return FALSE;
  99.         while (ax == 0)    {
  100.             if (!dirs || (ff.ff_attrib & FA_DIREC) && strcmp(ff.ff_name, "."))    {
  101.                 dirlist = DFrealloc(dirlist, sizeof(char *)*(i+1));
  102.                 dirlist[i] = DFmalloc(strlen(ff.ff_name)+1);
  103.                 strcpy(dirlist[i++], ff.ff_name);
  104.             }
  105.             ax = findnext(&ff);
  106.         }
  107.         if (dirlist != NULL)    {
  108.             int j;
  109.             /* -- sort file or directory list box data -- */
  110.             qsort(dirlist, i, sizeof(void *), dircmp);
  111.             /* ---- send sorted list to list box ---- */
  112.             for (j = 0; j < i; j++)    {
  113.                 SendMessage(lwnd,ADDTEXT,(PARAM)dirlist[j],0);
  114.                 free(dirlist[j]);
  115.             }
  116.             free(dirlist);
  117.         }
  118.         SendMessage(lwnd, SHOW_WINDOW, 0, 0);
  119.     }
  120.     return TRUE;
  121. }
  122.  
  123. BOOL BuildFileList(WINDOW wnd, char *fspec)
  124. {
  125.     return BuildList(wnd, fspec, FALSE);
  126. }
  127.  
  128. void BuildDirectoryList(WINDOW wnd)
  129. {
  130.     BuildList(wnd, "*.*", TRUE);
  131. }
  132.  
  133. void BuildDriveList(WINDOW wnd)
  134. {
  135.     CTLWINDOW *ct = FindCommand(wnd->extension, ID_DRIVE,LISTBOX);
  136.     if (ct != NULL)    {
  137.         union REGS regs;
  138.         char drname[15];
  139.         unsigned int cd, dr;
  140.         WINDOW lwnd = ct->wnd;
  141.         SendMessage(lwnd, CLEARTEXT, 0, 0);
  142.  
  143.         cd = getdisk();
  144.         for (dr = 0; dr < 26; dr++)    {
  145.             unsigned ndr;
  146.             setdisk(dr);
  147.             ndr = getdisk();
  148.             if (ndr == dr)    {
  149.                 /* ----- test for remapped B drive ----- */
  150.                 if (dr == 1)    {
  151.                     regs.x.ax = 0x440e; /* IOCTL func 14 */
  152.                     regs.h.bl = dr+1;
  153.                     int86(DOS, ®s, ®s);
  154.                     if (regs.h.al != 0)
  155.                         continue;
  156.                 }
  157.  
  158.                 sprintf(drname, "%c:", dr+'A');
  159.  
  160.                 /* ---- test for network or RAM disk ---- */
  161.                 regs.x.ax = 0x4409;     /* IOCTL func 9 */
  162.                 regs.h.bl = dr+1;
  163.                 int86(DOS, ®s, ®s);
  164.                 if (!regs.x.cflag)    {
  165.                     if (regs.x.dx & 0x1000)
  166.                         strcat(drname, " (Net)");
  167.                     else if (regs.x.dx == 0x0800)
  168.                         strcat(drname, " (RAM)");
  169.                 }
  170.                 SendMessage(lwnd,ADDTEXT,(PARAM)drname,0);
  171.             }
  172.         }
  173.         SendMessage(lwnd, SHOW_WINDOW, 0, 0);
  174.         setdisk(cd);
  175.     }
  176. }
  177.  
  178. void BuildPathDisplay(WINDOW wnd)
  179. {
  180.     CTLWINDOW *ct = FindCommand(wnd->extension, ID_PATH,TEXT);
  181.     if (ct != NULL)    {
  182.         int len;
  183.         WINDOW lwnd = ct->wnd;
  184.         CreatePath(path, "*.*", FALSE, FALSE);
  185.         len = strlen(path);
  186.         if (len > 3)
  187.             path[len-1] = '\0';
  188.            SendMessage(lwnd,SETTEXT,(PARAM)path,0);
  189.         SendMessage(lwnd, PAINT, 0, 0);
  190.     }
  191. }
  192.  
  193.  
  194.